package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CaseDao;
import com.util.Log;

@WebServlet(urlPatterns={"/admin/ManageCase"})
public class ManageCase extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final String CASE_PUBLISH = "CASE_PUBLISH";
	private static final String CASE_REVOKE = "CASE_REVOKE";
	private static final String CASE_DELETE = "CASE_DELETE";
	

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String manageType = request.getParameter("manageType");
		String caseId = request.getParameter("caseId");
		String message = "案例管理失败";
		Boolean daoResult = false;
		
		if(!(manageType.equals("")) && !(caseId.equals(""))) {
			CaseDao dao = new CaseDao();
			
			if(manageType.equals(CASE_PUBLISH)) {
				daoResult = dao.updateCasePublishStatus(Integer.parseInt(caseId), 1);
			} else if(manageType.equals(CASE_REVOKE)) {
				daoResult = dao.updateCasePublishStatus(Integer.parseInt(caseId), 0);
			} else if(manageType.equals(CASE_DELETE)) {
				if(dao.deleteCase("caseId=" + Integer.parseInt(caseId) + " ") >= 0) {
					daoResult = true;
				}
			}
			
			if (daoResult)
				message = "案例管理操作成功";
			dao.closeAll();
		} else {
			message = "案例管理请求参数错误";
		}
		
		String json = "{\"status\":\"" + message + "\"}";
		Log.write(json);
		out.println(json);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
